package controller;
import models.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
/*@RequestMapping("/register_Controller")*/
public class RegisterController {
    @Autowired
    private UserService userService;
    @GetMapping("/register")
    public String showRegisterPage() {
        return "Register"; // 返回视图名称，Spring MVC会解析为对应的JSP页面
    }


    @RequestMapping("/register_action")
    public void processRegistration(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 在这里处理注册逻辑，例如将用户信息保存到数据库中
        String name = request.getParameter("username");
        String password  = request.getParameter("password");
        String email = request.getParameter("email");

        User existUser = userService.getUserByUsername(name);
        User newUser =  new User();
        if (existUser!=null)
            response.getWriter().write("Account exists");
        else
        {
            newUser = userService.register(name, password, email);
            if (newUser!=null)
                response.getWriter().write("success");
            else response.getWriter().write("register failed");
        }



    }
}